// 导入请求方法
import { get, post } from '../../utils/request.js'
Page({
	data: {
		bannerList: [], //轮播图列表
		homeSearchisShow: true, // 是否显示搜索框
		homePlaceholder: '请输入需要查找的商品名称', // 搜索框提示的内容
		cityName: '', // 城市名称
		activityImgUrl: '/images/pages/home/home_activity.png',
		newGoodsImgUrl: '/images/pages/home/newgoods.png',
		hotGoodsImgUrl: '/images/pages/home/hotgoods.png',
		channelList: [],
		brandList: [],
		newGoodsList: [],
		hotGoodsList: [],
		topIconIsShow: false, // 控制滚动到top的icon的显示
		allGoodsList: [],
		count: 10,
		flag: 0 // 控制数据提示框是否出现
	},
	onLoad(res) {
		this._loadSwiperlist()
		this.getData()
		this.getHomeGoodsData()
		this.judgeDataLength()
	},
	onShow: function() { // 这里因为触发的是onshow 而不是onload 所以需要在onshow里面获取cityName
		this.getCityName()
	},
	onTabItemTap () { // 点击tabbar触发的事件
		// 进入社区页刷新一次
		this.judgeDataLength()
	},
	judgeDataLength () {
		setTimeout(() => { // 因为数据是异步获取的 这里使用定时器来解决 延迟执行
			const dataA = this.data.flag
			// console.log(dataA)
			if ( dataA == 0 ) {
				wx.hideLoading()
				wx.showModal({
					title: '错误提示',
					content: '获取数据失败，请联系管理员！',
					success(res) {
						if (res.confirm) {
							wx.switchTab({
								url: '/pages/main/main'
							})
						}
					}
				})
			}
		}, 1000)
	},
	_loadSwiperlist() { // 获取banner数据
		wx.cloud.callFunction({ // 连接云函数shop-swiper
			name: 'shop-swiper'
		}).then(res => {
			// console.log(res)
			let data = res.result.swiperList.data
			this.setData({
				bannerList: data
			})
		})
	},
	getCityName() { // 从storage中获取cityName
		const cityName = wx.getStorageSync('cityName')
		this.setData({
			cityName: cityName || '北京'
		})
		// console.log(cityName)
	},
	async getData () { // 异步获取
		try{ // 异常捕获 以设置提示框是否出现
			const result = await get('/index/index')
			this.setData({
				channelList: result.channel,
				brandList: result.brandList,
				newGoodsList: result.newGoods,
				hotGoodsList: result.hotGoods,
				flag: 1
			})
			// console.log(this.data.flag)
		}catch(e){
			this.setData({
				flag: 0
			})
		}
	},
	async getHomeGoodsData () {
		const allGoodsList = await get('/goods/getAllGoodsList', {
			count: this.data.count
		})
		// console.log(allGoodsList)
		this.setData({
			allGoodsList: allGoodsList.data
		})
		wx.stopPullDownRefresh()
		// console.log(this.data.allGoodsList)
	},
	goToClassify (e) {
		const id = e.target.dataset.id
		console.log(id)
		wx.navigateTo({
			url: `/pages/classify/classify?id=${id}`
		})
	},
	homeGoToClassify () {
		wx.navigateTo({
			url: `/pages/classify/classify?id=1012000`
		})
	},
	homeSearch () {
		wx.navigateTo({
			url: '/pages/homesearch/homesearch'
		})
	},
	goToDetail (e) {
		let id = e.currentTarget.dataset.id
		// console.log(id)
		wx.navigateTo({
			url: `/pages/goodsdetail/goodsdetail?id=${id}`
		})
	},
	onPageScroll(e) { // page页面的监听滚动的事件
		if (e.scrollTop > 100) { // 如果滚动大于100 就显示top组件
			this.setData({
				topIconIsShow: true
			});
		} else {
			this.setData({
				topIconIsShow: false
			});
		}
	},
	onPullDownRefresh() { // 下拉刷新
		const count = 10
		this.setData({
			count: count,
			allGoodsList: []
		})
		this.getHomeGoodsData()
	},
	onReachBottom(e) { // 触底加载
		// console.log(count, this.data.count)
		const data = this.data.count += 10
		this.getHomeGoodsData()
		// console.log(this.data.allGoodsList)
	},
})
